package com.lemote.action.schedule;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lemote.core.Page;
import com.lemote.entity.ScheuleEntity;
import com.lemote.service.schedule.ScheduleService;
import com.lemote.utils.encode.EncodeUtils;

/**
 * 
 * @author slaton
 * @time 2011-7-13 下午08:32:42
 * @Version:0.0.1
 * @since smt
 */
@Component("scheduleAction")
public class ScheduleAction {

	private  ScheuleEntity scheuleEntity;

	private Page<ScheuleEntity> page;
	
	private ScheduleService scheduleService;
	
	private String file;
	
	public String list(){
		if (page==null) {
			page = new Page<ScheuleEntity>(20);
		}
		page = scheduleService.fileList(page);
		return "success";
	}
	
	
	public String recove(){
		setBackUrl("permission/list.htm");
		if (file!=null) {
			scheduleService.recove(new File(new String(EncodeUtils.base64Decode(file))));
		}
		return "globalSuccess";
	}
	
	private void setBackUrl(String url) {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ctx = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
		request.getSession().setAttribute("backUrl",ctx+url);
				
	}


	public String delete(){
		if (file!=null) {
			File f = new File(new String(EncodeUtils.base64Decode(file)));
			if (f.exists()) {
				f.delete();
			}
		}
		return "success";
	}
	
	public ScheuleEntity getScheuleEntity() {
		return scheuleEntity;
	}

	public void setScheuleEntity(ScheuleEntity scheuleEntity) {
		this.scheuleEntity = scheuleEntity;
	}

	public Page<ScheuleEntity> getPage() {
		return page;
	}

	public void setPage(Page<ScheuleEntity> page) {
		this.page = page;
	}

	@Autowired
	public void setScheduleService(ScheduleService scheduleService) {
		this.scheduleService = scheduleService;
	}


	public String getFile() {
		return file;
	}
	public void setFile(String file) {
		this.file = file;
	}
	
	
}
